# # from multiprocessing import Process,Lock
# # import time
# # import json
# # def search(name):
# #     time.sleep(1)
# #     res = json.load(open('piao.txt','r',encoding='utf-8'))
# #     print(f'{name}正在查看剩余票数，当前剩余票数为：{res["count.txt"]}')
# # def fun(name):
# #     # Lock.acquire()
# #     # print(name)
# #     rev = json.load(open('piao.txt','r',encoding='utf-8'))
# #     if rev['count.txt']>0:
# #         rev['count.txt'] -= 1
# #         print(f"{name}取票成功，还剩{rev['count.txt']}")
# #         json.dump(rev,open('piao.txt,'w',encoding='utf-8'))
# #     else:
# #         print('取票失败,当前票数不足')
# #     time.sleep(2)
# #     # Lock.release()
# #     # print(123)
# #
# # def func(name):
# #     search(name)
# #     fun(name)
# # if __name__ == '__main__':
# #     # 模拟五个人同时抢票（开启五个进程）
# #     for i in range(5):
# #         Process(target=func, args=('石头', )).start()
# # from multiprocessing import Process
# # import time
# # import json
# #
# #
# # # 查看剩余票数
# # def search(name):
# #     time.sleep(1)
# #     # 读取文件获取当前票数，使用json.load()去解析成字典对象
# #     res = json.load(open('count.txt.txt', 'r', encoding='utf-8'))
# #     print(f'{name}正在查看剩余票数，当前剩余票数为：{res["count.txt"]}')
# #
# #
# # # 买票
# # def get(name):
# #     # 读取文件获取当前票数，使用json.load()去解析成字典对象
# #     res = json.load(open('count.txt.txt', 'r', encoding='utf-8'))
# #     if res['count.txt'] > 0:
# #         res['count.txt'] -= 1
# #         time.sleep(2)
# #         print(f'{name}您已购票成功，当前剩余票数为：{res["count.txt"]}')
# #         json.dump(res, open('count.txt.txt', 'w', encoding='utf-8'))
# #
# # def func(name):
# #     search(name)
# #     get(name)
#
#
# # if __name__ == '__main__':
# #     # 模拟五个人同时抢票（开启五个进程）
# #     for i in range(5):
# #         Process(target=func, args=('石头', )).start()
# # '''
# #     有数据共享，有竞争，竞争带来的问题就是数据混乱。
# #     五个进程同时取到的剩余票数都是100，所有结果会是99，这个文件在这个时间段还未被更改。
# # '''
#
# from multiprocessing import Process, Lock
# import time
# import json
#
#
# # 查看剩余票数
# def search(name):
#     time.sleep(1)
#     # 读取文件获取当前票数，使用json.load()去解析成字典对象
#     res = json.load(open('count.txt', 'r', encoding='utf-8'))
#     print(f'{name}正在查看剩余票数，当前剩余票数为：{res["count.txt"]}')
#
#
# # 买票
# def get(name):
#     # 读取文件获取当前票数，使用json.load()去解析成字典对象
#     res = json.load(open('count.txt', 'r', encoding='utf-8'))
#     if res['count.txt'] > 0:
#         res['count.txt'] -= 1
#         time.sleep(2)
#         print(f'{name}您已购票成功，当前剩余票数为：{res["count.txt"]}')
#         json.dump(res, open('count.txt', 'w', encoding='utf-8'))


# # def func(name, lock):
# #     search(name)
# #     # 加锁
# #     lock.acquire()
# #     get(name)
# #     # 解锁
# #     lock.release()
# #
# #
# # if __name__ == '__main__':
# #     # 模拟五个人同时抢票（开启五个进程）
# #     # 创建锁
# #     lock = Lock()
# #     for i in range(5):
# #         Process(target=func, args=('石头', lock)).start()
#
#
# # from multiprocessing import Process,Semaphore
# # import time
# #
# #
# # def block(name,s):
# #     s.acquire()
# #     print(f'{name}在关禁闭')
# #     time.sleep(1)
# #
# #     print(f'{name}以释放')
# #     s.release()
# #
# #
# #
# # if __name__ == '__main__':
# #     s = Semaphore(1)
# #     for i in range(5):
# #         Process(target=block,args=(i,s)).start()
# 
#
# # from multiprocessing import Queue
# # import queue
# #
# # q = Queue(3)
# #
# # q.put('1')
# # q.put('2')
# #
# #
# # q.put_nowait('3')
# # try:
# #     q.put_nowait('4')
# # except queue.Full:
# #     print('消息已经满了')
# #
# #
# # print(q.full())
# #
# # print(q.qsize())
# # print(q.get())
# # print(q.get())
# # print(q.get())
# # try:
# #     print(q.get_nowait())
# #
# # except queue.Empty:
# #     print('队列已经空了')
# #
# # print(q.empty())
# import time
# from threading import Thread
# import threading
# # def sayhello():
# #     print(123)
# #     time.sleep(2)
# #
# # if __name__ == '__main__':
# #     for i in range(5):
# #         sayhello()
#
# # def sayhello():
# #     print(123)
# #     time.sleep(3)
# #
# # if __name__ == '__main__':
# #     for i in range(5):
# #         # time.sleep(2)
# #         Thread(target=sayhello).start()
#
# # class mythread(threading.Thread):
# #     def __init__(self,num):
# #         super().__init__()
# #         self.num = num
# #     def lecture(self):
# #         for i in range(5):
# #             print(123)
# #
# #     def run(self):
# #         time.sleep(2)
# #         self.lecture()
# #
# # if __name__ == '__main__':
# #     my = mythread(5)
# #     my.start()
# from multiprocessing import Process
#
# # class myprocess(Process):
# #     def __init__(self,name):
# #         super().__init__()
# #         self.name = name
# #
# #     def playgame(self,name):
# #         for i in range(5):
# #             print(name)
# #             time.sleep(2)
# #
# #     def run(self):
# #         self.playgame(self.name)
# # if __name__ == '__main__':
# #     myprocess("123").start()
# from multiprocessing import Queue
# import queue
#
# # 初始化Queue对象，有一个参数，是指定消息数量的，数量没有上限，知道内存存满
# q = Queue(3)
# # 存入消息
# q.put('1')
# q.put('2')
# # 队列中还有没有空间存放，没有空间存放会抛出queue.Full异常
# q.put_nowait('3')
#
# try:
#     q.put_nowait('4')
# except queue.Full:
#     print('消息满了')
#
# # 判断队列是否超载
# print(q.full())
#
# # 获取队列中有多少数据
# print(q.qsize())
#
# # 获取队列中的数据
# print(q.get())
# print(q.get())
# print(q.get())
#
# try:
#     # get_nowait 队列中没有消息了，会抛出queue.Empty异常
#     print(q.get_nowait())
# except queue.Empty:
#     print('队列已经空了')
#
# # 判断队列是否为空
# print(q.empty())
# from multiprocessing import Process, Semaphore
# import time
#
#
# def black(name, s):
#     s.acquire()
#     print(f'{name}正在关禁闭')
#     time.sleep(3)
#     print(f'{name}已越狱')
#     s.release()
#
#
# if __name__ == '__main__':
#     # 创建信号量，设置多少个并行
#     s = Semaphore(1)
#     list = ['tom', 'joker', 'z1', 'z2', 'z3', 'z4']
#     for name in list:
#         Process(target=black, args=(f'{name}', s)).start()
#
#     import time
#     from threading import Thread
#
#
#     def sayHello():
#     print('Hello')
#     time.sleep(1)
#
#
#     # 主进程
#     if __name__ == '__main__':
#     for i in range(5):
#         # 创建子线程
#         Thread(target=sayHello).start()
#     import time
#
#     def sayHello():
#     print('Hello')
#     time.sleep(1)
#
#     if __name__ == '__main__':
#     for i in range(5):
#     sayHello()
#     from threading import Thread
#
#     age = 20
#
#
#     def func():
#     # 能在函数值修改属性的值
#     global age
#     age = 25
#     print(f'子线程中的年龄{age}')
#
#
#     if __name__ == '__main__':
#     Thread(target=func).start()
#
#     print(f'主线程中的年龄{age}')
# import threading, time
#
#
# # 自定义线程类(必须继承Thread)
# class MyThread(threading.Thread):
#     def __init__(self, num):
#         # 必须先调用父类的init，否则会报错
#         super().__init__()
#         self.num = num
#
#     def lecture(self):
#         for i in range(5):
#             print('感谢大家收看，由石头老师给大家带来的自定义线程')
#             print('大家晚安')
#
#     def run(self):
#         self.lecture()
#
#
# if __name__ == '__main__':
#     myThread = MyThread(5)
#     myThread.start()
# from multiprocessing import Process
# import time
# import os
#
#
# class MyProcess(Process):
#
#     def __init__(self, name):
#         super().__init__()
#         self.name = name
#
#     # 进程任务
#     def playGames(self, name):
#         print("子进程(%s)开始执行，父进程(%s)"
#               % (os.getpid(), os.getppid()))
#         for i in range(5):
#             print(f'{name}已经单杀对方{i + 1}次')
#             time.sleep(2)
#
#     # 必须重写run方法
#     # 所有的任务必须在run方法中调用
#     def run(self):
#         self.playGames(self.name)
#
# '''
#     随便写的名字，类名、函数名、变量名
# '''
# if __name__ == '__main__':
#     MyProcess('石头').start()

# from multiprocessing import Process, Lock
# import time
# import json
#
#
# # 查看剩余票数
# def search(name):
#     time.sleep(1)
#     # 读取文件获取当前票数，使用json.load()去解析成字典对象
#     res = json.load(open('count.txt', 'r', encoding='utf-8'))
#     print(f'{name}正在查看剩余票数，当前剩余票数为：{res["count"]}')
#
#
# # 买票
# def get(name):
#     # 读取文件获取当前票数，使用json.load()去解析成字典对象
#     res = json.load(open('count.txt', 'r', encoding='utf-8'))
#     if res['count'] > 0:
#         res['count'] -= 1
#         time.sleep(2)
#         print(f'{name}您已购票成功，当前剩余票数为：{res["count"]}')
#         json.dump(res, open('count.txt', 'w', encoding='utf-8'))
#
#
# def func(name, lock):
#     search(name)
#     # 加锁
#     lock.acquire()
#     get(name)
#     # 解锁
#     lock.release()
#
#
# if __name__ == '__main__':
#     # 模拟五个人同时抢票（开启五个进程）
#     # 创建锁
#     lock = Lock()
#     for i in range(5):
#         Process(target=func, args=('石头', lock)).start()
